package com.usvisatrack.core.factories;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.usvisatrack.core.common.DataObject;
import com.usvisatrack.core.common.Factory;
import com.usvisatrack.core.dao.model.UscisForm;
import com.usvisatrack.core.dao.model.UscisFormClassification;

/**
 * Item Data Factory
 * 
 * @author YuCheng Hu
 *
 */
public class UscisFormFactory extends Factory {
	private static Logger logger = LoggerFactory.getLogger(UscisFormFactory.class);

	private static final int LISTING_PAGE_SIZE = 20;
	public static final int DEFAULT_AUTH_EXPIRATION = 10;

	/**
	 * Get Visa OBJ
	 * 
	 * @param checkkeID
	 * @return
	 */
	public static UscisForm getUscisFormByCode(String uscisFormCode) {
		logger.debug("Get UscisForm OBJ by form code - [{}]", uscisFormCode);

		try {
			Factory.beginTransaction();

			Criteria criteria = Factory.createCriteria(UscisForm.class);
			criteria.add(Restrictions.ilike("uscisFormCode", "%" + uscisFormCode + "%"));
			criteria.setMaxResults(1);

			return (UscisForm) criteria.uniqueResult();
		} catch (Exception ex) {
			logger.error("Search UscisForm data error", ex);
		} finally {
			Factory.commitTransaction();
		}
		return null;
	}

	/**
	 * Get Visa OBJ
	 * 
	 * @param checkkeID
	 * @return
	 */
	public static UscisFormClassification getUscisFormClassification(UscisForm uscisForm, String formClassificationTitle,
			String formClassificationName) {

//		logger.debug("Get UscisFormClassification OBJ by formCode/formName - [{}]/[{}]", uscisForm.getUscisFormName(),
//				formClassificationName);

		try {
			Factory.beginTransaction();

			Criteria criteria = Factory.createCriteria(UscisFormClassification.class);
			criteria.add(Restrictions.eq("uscisFormClassificationTitle", formClassificationTitle));
			criteria.add(Restrictions.eq("uscisFormClassificationName", formClassificationName));

			criteria.setMaxResults(1);

			return (UscisFormClassification) criteria.uniqueResult();
		} catch (Exception ex) {
			logger.error("Search UscisFormClassification data error", ex);
		} finally {
			Factory.commitTransaction();
		}
		return null;
	}

	public static void save(DataObject object) {
		Factory.beginTransaction();
		Factory.save(object);
		Factory.commitTransaction();
	}

}